# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time        : 2021/10/10 21:48
@Author      : Albert Darren
@Contact     : 2563491540@qq.com
@File        : piecewise_scale.py
@Version     : Version 1.0.0
@Description : TODO
@Created By  : PyCharm
"""
from DIP_experiment_4.util import histogram,contrast_show
import numpy as np
from PIL import Image
img_path = "../experiment_fig/beans.png"
my_font = "C:/Windows/Fonts/simsun.ttc"
origin_im = Image.open(img_path)  # RGBA
r, g, b, a = origin_im.split()
r_array = np.array(r.getdata()).reshape(r.size)
stretched_r = np.piecewise(r_array, [r_array < 91, r_array > 114],
                           [lambda r_array: 93 / 91 * r_array, lambda r_array: r_array,
                            lambda r_array: 39 / 23 * r_array - 61.30])
stretched_r_img = Image.fromarray(stretched_r)
im_hist_dict = {"beans原始图像的r通道": r, "beans对比度拉伸图像的r通道": stretched_r_img}
histogram(image_objs=im_hist_dict, subplot_size=(1, 2), wspace=0.4,
          xticks=np.arange(0, 255, 10), xrotation=45, font=my_font)
im_dict={"原始图像的r通道":r,"beans对比度拉伸图像的r通道":stretched_r_img}
contrast_show(im_dict,(1,2),font=my_font)
